// Chat Program for PCMod (CLIENT)

PROG.PName = "AChat"
PROG.Author = "[GU]|ZLC|thomasfn"

PROG.NiceName = "AChat"

function PROG:BuildWindow( WindowName, Desktop, Object )
	// Here we build the derma window.
	// Note the Desktop string is needed for every operation involving derma.
	if (WindowName == "Main") then
		// Add a title label
		//DInterface:AddLabel( "lblTitle", Desktop, "AChat (Anonymous Chat Program)", 5, 5 )
		
		// Add the chat area
		// DInterface:AddHTML( "htchat", Desktop, "", 5, 25, Object:GetWide()-10, Object:GetTall()-55 )
		self.ChatArea = vgui.Create( "ChatView" )
		self.ChatArea:SetParent( Object )
		self.ChatArea:SetPos( 5, 25 )
		self.ChatArea:SetSize( Object:GetWide()-10, Object:GetTall()-55 )
		
		// Add the IP textbox, chat textbox and send button
		DInterface:AddTextbox( "txtIP", Desktop, "192.168.0.1", 5, Object:GetTall()-25, 100, 20 )
		DInterface:AddTextbox( "txtChat", Desktop, "", 105, Object:GetTall()-25, Object:GetWide()-190, 20 )
		DInterface:AddButton( "btnSend", Desktop, "Send", Object:GetWide()-80, Object:GetTall()-25, 70, 20, function()
			local chat = DInterface:GetTextboxValue( "txtChat", Desktop )
			if (chat == "/clear") then
				SendCommand( "clear" )
			else
				SendCommand( "sendchat", DInterface:GetTextboxValue( "txtIP", Desktop ), chat )
			end
			self:UpdateChat( Desktop )
			DInterface:SetTextboxText( "txtChat", Desktop, "" )
		end )
		
		self.Desktop = Desktop
		
		timer.Create( "PCMod_AChat_Update", 1, 0, function()
			if (C_PRO) and (PCLoaded) then
				if (C_PRO.PName == "AChat") then
					C_PRO:UpdateChat( Desktop )
				else
					timer.Destroy( "PCMod_AChat_Update" )
				end
			else
				timer.Destroy( "PCMod_AChat_Update" )
			end
		end )
	end
	
	// Add a quit button
	DInterface:AddButton( "btnQuit", Desktop, "X", Object:GetWide()-20, 0, 20, 20, function() SendCommand( "quit" ); end )
	
	// Update our chat area
	self:UpdateChat( Desktop )
end

function PROG:ClearWindow( Desktop )
	// Here we destroy EVERY derma control that MIGHT exist.
	DInterface:RemoveLabel( "lblTitle", Desktop )
	// DInterface:RemoveHTML( "htchat", Desktop )
	
	DInterface:RemoveTextbox( "txtIP", Desktop )
	DInterface:RemoveTextbox( "txtChat", Desktop )
	DInterface:RemoveButton( "btnSend", Desktop )
	
	if (self.ChatArea) then
		self.ChatArea:Remove()
		self.ChatArea = nil
	end
	
	DInterface:RemoveButton( "btnQuit", Desktop )
	
	timer.Destroy( "PCMod_AChat_Update" )
end

// Data[1] = Tag
// Data[2] = Content
function PROG:DataRecieved( Desktop, Data )
	// The server has sent us some data
	if (Data[1] == "update") then self:UpdateChat( Desktop ) end
	if (Data[1] == "lastip") then DInterface:SetTextboxText( "txtIP", Desktop, Data[2] ) end
end

// Custom Functions
function PROG:UpdateChat( Desktop )
	local Chat = ""
	local pc = CurrentPC
	local msgcount = pc:GetNWInt( "achat_msgs" )
	for n=1,msgcount do
		local str2 = pc:GetNWString( "achat_msg_" .. tostring(n) .. "_2" )
		if (!str2) then str2 = "" end
		local str = pc:GetNWString( "achat_msg_" .. tostring(n) .. "_1" ) .. str2
		if (str) then Chat = Chat .. str .. "#" end
	end
	// DInterface:SetHTMLText( "htchat", Desktop, Chat )
	self.ChatArea:SetChatText( Chat, "#" )
end

function PROG:Paint( x, y, w, h )
	draw.RoundedBox( 4, x+(w*0.05), y+(h*0.1), w*0.9, h*0.75, Color( 255, 255, 255, 200 ) )
end